﻿#region Apache License Version 2.0
/*----------------------------------------------------------------

Copyright 2025 Suzhou Senparc Network Technology Co.,Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions
and limitations under the License.

Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md

----------------------------------------------------------------*/
#endregion Apache License Version 2.0

/*----------------------------------------------------------------
    Copyright (C) 2025 Senparc
    
    文件名：TextResponseLimitOptions.cs
    文件功能描述：限制返回消息长度，如果超出，则调用客服接口进行发送
    
    
    创建标识：Senparc - 20230709

----------------------------------------------------------------*/


using Senparc.NeuChar.Entities;
using Senparc.NeuChar.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Senparc.NeuChar.MessageHandlers
{
    /// <summary>
    /// 限制返回消息长度，如果超出，则调用客服接口进行发送
    /// </summary>
    public class TextResponseLimitOptions
    {
        /// <summary>
        /// 需要限制的长度
        /// </summary>
        public int MaxTextBytesLimit { get; set; }
        /// <summary>
        /// 已经注册完的 AppId 或 AccessToken
        /// </summary>
        public string AccessTokenOrAppId { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="maxTextBytesLimit">需要限制的长度（字节），默认为 2048</param>
        /// <param name="accessTokenOrAppId"></param>
        public TextResponseLimitOptions(int maxTextBytesLimit = 2048, string accessTokenOrAppId = null)
        {
            MaxTextBytesLimit = maxTextBytesLimit;
            AccessTokenOrAppId = accessTokenOrAppId;
        }

        /// <summary>
        /// 设置 accessTokenOrAppId
        /// </summary>
        /// <param name="accessTokenOrAppId"></param>
        public void SetAccessTokenOrAppId(string accessTokenOrAppId)
        {
            AccessTokenOrAppId = accessTokenOrAppId;
        }

        /// <summary>
        /// 检查消息是否超出限制，如果超出，则使用客服消息接口，并且返回 false，否则返回 true
        /// </summary>
        /// <param name="responseMessageText">文字回复消息</param>
        /// <param name="messageHandler">MessageHandler</param>
        /// <param name="openId"></param>
        /// <returns></returns>
        public async Task<bool> CheckOrSendCustomMessage(IResponseMessageText responseMessageText, IMessageHandlerEnlightener messageHandler, string openId)
        {
            var origStr = responseMessageText.Content;
            var bytesLength = MessageHandlerHelper.GetBytes(origStr);
            if (bytesLength > MaxTextBytesLimit)
            {
                //使用客服消息

                //TODO:判断 accessTokenOrAppId 是否为空，可以抛出异常

                await messageHandler.ApiEnlightener.SendText(AccessTokenOrAppId, openId, origStr, MaxTextBytesLimit);

                return false;
            }

            return true;
        }
    }
}
